﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using XiaoMan.API.Models;
using XiaoMan.Model.Models;
using XiaoMan.BLL;
using XiaoMan.Service;
using XiaoMan.Common.API;
using XiaoMan.API.Filter;

namespace XiaoMan.API.Controllers
{
    [ApiHandleError]
    [RoutePrefix("api/auth")]
    public class AuthController : ApiController
    {
        /// <summary>
        /// 登陆验证
        /// </summary>
        /// <param name="_logonInfo"></param>
        /// <returns></returns>        
        [Route("applogin")]
        [HttpPost]
        public IHttpActionResult UserLogon([FromBody]LogOnModel _logonInfo)
        {
            try
            {
                if (string.IsNullOrEmpty(_logonInfo.UserName) || string.IsNullOrEmpty(_logonInfo.Password))
                {
                    return Json(new { message = "登录失败,检查用户名或密码是否正确" });
                }
                var _userInfo = new Sys_UsersService().CheckLogOn(_logonInfo.UserName, _logonInfo.Password);
                if (_userInfo == null)
                {
                    return Json(new { message = "登录失败,检查用户名或密码是否正确" });
                }
                var _token = MD5Manager.Gen_MD5(_userInfo.Password + DateTime.UtcNow.ToString("yyyyMMdd") + MD5Manager.GetNonCryptoRandomDataAsBase64(128), false, false);
                CacheManager.Set(_token, _userInfo, new TimeSpan(24, 0, 0));//过期时间  24H
                return Json(new
                {
                    token = _token,
                    userInfo = _userInfo
                });
            }
            catch (Exception)
            {

                return Json(new { message = "登录失败,检查用户名或密码是否正确" });
            }
        }
        /// <summary>
        /// 小程序登陆
        /// </summary>
        /// <param name="wxopenid"></param>
        /// <returns></returns>
        [Route("wxlogin")]
        [HttpPost]
        public IHttpActionResult UserLogon(dynamic obj)
        {
            string wxopenid = Convert.ToString(obj.wxopenid);
            Sys_UsersService sus = new Sys_UsersService();
            if (string.IsNullOrWhiteSpace(wxopenid))
            {
                return Json(new { message = "登陆失败" });
            }
            var _userInfo = sus.CheckLogOn(wxopenid);
            if (_userInfo == null)
            {
                sus.Add(new Sys_Users { WeChat = wxopenid, CreateTime = DateTime.Now });
                _userInfo = sus.CheckLogOn(wxopenid);
            }
            var _token = MD5Manager.Gen_MD5(wxopenid + DateTime.UtcNow.ToString("yyyyMMdd") + MD5Manager.GetNonCryptoRandomDataAsBase64(128), false, false);
            CacheManager.Set(_token, _userInfo, new TimeSpan(24, 0, 0));//过期时间  24H           
            return Json(new
            {
                token = _token,
                userinfo = _userInfo
            });
        }
    }
}
